#include "slidemenu.h"
#include "datautils.h"

#include <QComboBox>
#include <QPushButton>
#include <QVBoxLayout>
#include <QScrollArea>

SlideMenu::SlideMenu(QWidget *parent)
    : QWidget(parent),
    m_layout(new QVBoxLayout(this)),
    m_continentCombo(new QComboBox(this)),
    m_scrollArea(new QScrollArea(this)),
    m_cityContainer(new QWidget)
{
    setAttribute(Qt::WA_StyledBackground);
    setFixedWidth(240);

    // 配置滚动区域
    m_scrollArea->setWidgetResizable(true);
    m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); // 显示垂直滚动条
    m_scrollArea->setWidget(m_cityContainer);

    // 容器布局
    QVBoxLayout* containerLayout = new QVBoxLayout(m_cityContainer);
    containerLayout->setSpacing(4);
    containerLayout->setContentsMargins(2, 2, 2, 2);

    initMenuUI();
    loadCitiesData();
}

void SlideMenu::initMenuUI() {
    setStyleSheet(R"(
        SlideMenu {
            background: #ffffff;
            border-right: 1px solid #e0e0e0;
        }
        QScrollArea {
            background: transparent;
            border: none;
        }
        QComboBox {
            padding: 8px;
            margin: 4px;
            font: 14px 'Microsoft YaHei';
            background: white;
            border: 1px solid #ddd;
        }
        QPushButton {
            padding: 10px;
            margin: 2px;
            font: 14px 'Microsoft YaHei';
            background: white;
            border: none;
            border-radius: 4px;
            text-align: center;  /* 确保文字居中 */
            min-height: 36px;
        }
        QPushButton:hover {
            background: #f0f0f0;  /* 匹配截图中的浅灰色调 */
            border-color: #d0d0d0; /* 添加细边框增强反馈 */
        }
        QPushButton:pressed {
            background: #e0e0e0;
        }
    )");

    m_layout->setContentsMargins(6, 6, 6, 6);
    m_layout->addWidget(m_continentCombo);
    m_layout->addWidget(m_scrollArea);

    connect(m_continentCombo, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, &SlideMenu::updateCityList);
}

void SlideMenu::loadCitiesData() {
    m_cityData = {
        {"中国", {"北京", "上海", "广州", "深圳", "成都", "重庆", "杭州"}},
        {"亚洲", {"Tokyo", "Seoul", "Singapore", "Kuala Lumpur", "Jakarta", "Mumbai"}},
        {"欧洲", {"London", "Paris", "Berlin", "Madrid", "Rome", "Amsterdam"}},
        {"澳洲", {"Sydney", "Melbourne", "Auckland", "Brisbane", "Perth", "Adelaide", "Canberra"}},
        {"美洲", {"New York", "Los Angeles", "Toronto", "Chicago", "Lima"}},
        {"非洲", {"Nairobi", "Cape Town", "Johannesburg", "Lagos", "Accra"}}
    };
    m_continentCombo->addItems(m_cityData.keys());
    updateCityList();
}

void SlideMenu::updateCityList() {
    // 清空旧按钮
    QLayoutItem* child;
    while ((child = m_cityContainer->layout()->takeAt(0)) != nullptr) {
        delete child->widget();
        delete child;
    }

    // 创建新按钮
    const QString continent = m_continentCombo->currentText();
    for (const auto &city : m_cityData[continent]) {
        auto btn = new QPushButton(city);
        connect(btn, &QPushButton::clicked, [this, city] {
            emit citySelected(CityConverter::toPinyin(city));
        });
        m_cityContainer->layout()->addWidget(btn);
    }
    m_cityContainer->adjustSize(); // 自动调整内容大小
}

void SlideMenu::syncParentSize() {
    if (parentWidget()) {
        setFixedHeight(parentWidget()->height());
        // 计算滚动区域可用高度：父窗口高度 - 下拉框高度 - 边距
        m_scrollArea->setFixedHeight(parentWidget()->height() - 60);
        move(-width(), 0);
    }
}
